package es.ibm.santillana.mdm.nonpersistentrule;

import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.collaboration.CollaborationItem;
import com.ibm.pim.extensionpoints.CategoryNonPersistedAttributeRuleFunctionArguments;
import com.ibm.pim.extensionpoints.CollaborationCategoryNonPersistedAttributeRuleFunctionArguments;
import com.ibm.pim.extensionpoints.CollaborationItemNonPersistedAttributeRuleFunctionArguments;
import com.ibm.pim.extensionpoints.ItemNonPersistedAttributeRuleFunctionArguments;
import com.ibm.pim.extensionpoints.NonPersistedAttributeRuleFunction;
import com.ibm.pim.spec.Spec;

import es.ibm.santillana.mdm.common.bussinesmodel.ConfigModel;



public class CicloRule implements NonPersistedAttributeRuleFunction{

	public CicloRule() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Object rule(ItemNonPersistedAttributeRuleFunctionArguments arg0) {
		System.out.println("/Nivel_Escolar/Curso");
		String sSpecName = arg0.getAttributeInstance().getAttributeDefinition().getSpec().getName();
		
		Spec specSearch = ConfigModel.transformaSpec(sSpecName);
		String sPathCurso = specSearch.getName() + "/Nivel_Escolar/Curso";
		
		Item itm = arg0.getItem();
		Item itmCurso = (Item) itm.getAttributeValue(sPathCurso);
		if (itmCurso != null) {
			System.out.println("item cursp");
			Item itmCiclo = (Item)  itmCurso.getAttributeValue("Cursos/Code_Ciclo");
			if (itmCiclo!=null) {
				System.out.println("ciclo");
				arg0.getAttributeInstance().setValue(itmCiclo.getAttributeValue("Ciclos/Valor/es_ES"));
					
				}
				
			}
		
		return null;
		
	}

	@Override
	public Object rule(
			CollaborationItemNonPersistedAttributeRuleFunctionArguments arg0) {
		String sSpecName = arg0.getAttributeInstance().getAttributeDefinition().getSpec().getName();
		Spec specSearch = ConfigModel.transformaSpec(sSpecName);
		String sPathCurso = specSearch.getName() + "/Nivel_Escolar/Curso";
		
		CollaborationItem itm = arg0.getCollaborationItem();
		Item itmCurso = (Item) itm.getAttributeValue(sPathCurso);
		if (itmCurso != null) {
			
			Item itmCiclo = (Item)  itmCurso.getAttributeValue("Cursos/Code_Ciclo");
			if (itmCiclo!=null) {
				
				arg0.getAttributeInstance().setValue(itmCiclo.getAttributeValue("Ciclos/Valor/es_ES"));
					
				}
				
			}
		
		return null;
		
	}

	@Override
	public Object rule(CategoryNonPersistedAttributeRuleFunctionArguments arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object rule(
			CollaborationCategoryNonPersistedAttributeRuleFunctionArguments arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
